<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">上传</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="upgradeList">
      <el-table-column label="文件名称" align="center" prop="fileName" />
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改文件配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="configRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="文件" prop="file">
          <el-upload action="" :show-file-list="true" :file-list="fileList" :http-request="httpRequest" :limit="1">
            <el-button type="success">
              <span>上传文件</span>
            </el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Upgrade">
import { listUpgrade, addUpgrade, delUpgrade } from "@/api/upgrade/list";

const { proxy } = getCurrentInstance();
// const { sys_yes_no } = proxy.useDict("sys_yes_no");

const upgradeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const fileList = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询文件列表 */
function getList() {
  loading.value = true;
  listUpgrade(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    upgradeList.value = response.rows;
    loading.value = false;
    total.value = response.total
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    file: undefined,
  };
  proxy.resetForm("configRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加文件";
}
/** 提交按钮 */
function httpRequest(file) {
  let upload = new FormData()
  upload.append("file", file.file)
  addUpgrade(upload).then(response => {
    proxy.$modal.msgSuccess("新增成功");
    open.value = false;
    getList();
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const fileIds = row.fileId || ids.value;
  proxy.$modal.confirm('是否确认删除文件编号为"' + fileIds + '"的数据项？').then(function () {
    return delUpgrade(fileIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

getList();
</script>
